package com.xxl.job.executor.service.jobhandler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hns.its.common.utils.Identities;
import com.hns.its.service.order.ItsBaseShippingScheduleService;
import com.xmkbjs.shippingSchedule.ScheduleStarter;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 任务Handler示例（Bean模式）
 * 
 * 开发步骤： 1、继承"IJobHandler"：“com.xxl.job.core.handler.IJobHandler”；
 * 2、注册到Spring容器：添加“@Component”注解，被Spring容器扫描为Bean实例；
 * 3、注册到执行器工厂：添加“@JobHandler(value
 * ="自定义jobhandler名称")”注解，注解value值对应的是调度中心新建任务的JobHandler属性的值。 4、执行日志：需要通过
 * "XxlJobLogger.log" 打印执行日志；
 * 
 * @author xuxueli 2015-12-19 19:43:36
 */
@JobHandler(value = "crawlerShippingScheduleJobHandler")
@Component
public class CrawlerShippingScheduleJobHandler extends IJobHandler {
	private static final Logger logger = LoggerFactory.getLogger(CrawlerShippingScheduleJobHandler.class);

	@Autowired
	private ItsBaseShippingScheduleService baseShippingScheduleService;

	@Override
	public ReturnT<String> execute(String param) throws Exception {
		XxlJobLogger.log("定时任务爬取船期......开始");

		String scheduleJsonStr = ScheduleStarter.getInstance().crawlAlltoJsonStr();
		JSONArray jsonArray = JSONArray.parseArray(scheduleJsonStr);
		if (jsonArray.size() > 0) {
			XxlJobLogger.log("船期共有：" + jsonArray.size() + "趟！");
			StringBuffer sql = new StringBuffer(
					"INSERT INTO its_base_shipping_schedule"
							+ "(id,ship_name,ship_cn_name,ship_timer,sailing_date,berth_date,leave_date,into_date,"
							+ "off_date,cut_off_date,crawl_date, crawl_wharf)"
							+ "VALUES");
			for (Object object : jsonArray) {
				JSONObject jsonObj = JSONObject.parseObject(object.toString());
				sql.append("('" + Identities.uuid2() + "',");
				sql.append("'" + jsonObj.get("shipEnName") + "',");
				sql.append("'" + jsonObj.get("shipName") + "',");
				sql.append("'" + jsonObj.get("shipTimer") + "',");
				if (jsonObj.get("berthDate") != null
						&& !jsonObj.get("berthDate").equals("")) {
					sql.append("'" + jsonObj.get("berthDate") + "',");
					sql.append("'" + jsonObj.get("berthDate") + "',");
				} else {
					sql.append("null,");
					sql.append("null,");
				}
				if (jsonObj.get("leaveDate") != null
						&& !jsonObj.get("leaveDate").equals("")) {
					sql.append("'" + jsonObj.get("leaveDate") + "',");
				} else {
					sql.append("null,");
				}
				if (jsonObj.get("intoDate") != null
						&& !jsonObj.get("intoDate").equals("")) {
					sql.append("'" + jsonObj.get("intoDate") + "',");
				} else {
					sql.append("null,");
				}
				if (jsonObj.get("offDate") != null
						&& !jsonObj.get("offDate").equals("")) {
					sql.append("'" + jsonObj.get("offDate") + "',");
				} else {
					sql.append("null,");
				}
				if (jsonObj.get("cutOffDate") != null
						&& !jsonObj.get("cutOffDate").equals("")) {
					sql.append("'" + jsonObj.get("cutOffDate") + "',");
				} else {
					sql.append("null,");
				}
				if (jsonObj.get("crawlDate") != null
						&& !jsonObj.get("crawlDate").equals("")) {
					sql.append("'" + jsonObj.get("crawlDate") + "',");
				} else {
					sql.append("null,");
				}
				sql.append("'" + jsonObj.get("dockCode") + "'),");
			}
			baseShippingScheduleService.saveCrawlerShippingSchedule((sql.substring(0, sql.length() - 1).toString()));
		}

		XxlJobLogger.log("定时任务爬取船期......结束");

		return new ReturnT<String>(ReturnT.SUCCESS_CODE, "定时任务爬取船期成功！");
	}

}
